﻿#pragma once

#include "../../MirControls/MirImageControl.h"
#include "../../MirControls/MirLabel.h"
#include "../../MirControls/MirButton.h"
#include "../../MirControls/MirControl.h"
#include "../../MirControls/MirAnimatedControl.h"
#include "../../../Shared/Data/IntelligentCreatureData.h"
#include "../../../Shared/Enums.h"
#include "../../MirControls/MirCheckBox.h"
#include <string>
#include <vector>
#include <any>
#include "../../../stringhelper.h"

//C# TO C++ CONVERTER NOTE: Forward class declarations:
namespace Client::MirScenes::Dialogs { class CreatureButton; }

using namespace Client::MirControls;
using namespace Client::MirGraphics;
using namespace Client::MirNetwork;
using namespace Client::MirSounds;
namespace C = ClientPackets;

namespace Client::MirScenes::Dialogs
{
    class IntelligentCreatureDialog final : public MirImageControl
    {
    public:
        MirImageControl *FullnessBG, *FullnessFG, *FullnessMin, *FullnessNow;
        MirImageControl *PearlImage, *BlackStoneImageBG, *BlackStoneImageFG;
        MirLabel *CreatureName, *CreatureDeadline, *CreaturePearls, *CreatureInfo, *CreatureInfo1, *CreatureInfo2, *CreatureMaintainFoodBuff, *HoverLabel;
        MirButton *CloseButton, *HelpPetButton, *CreatureRenameButton, *SummonButton, *DismissButton, *ReleaseButton;
        MirButton *AutomaticModeButton, *SemiAutoModeButton, *OptionsMenuButton;
        std::vector<CreatureButton*> CreatureButtons;
        int SelectedCreatureSlot = -1;
        MirControl *HoverLabelParent = nullptr;

    private:
        MirAnimatedControl *CreatureImage;
    public:
        long long SwitchAnimTime = 0;
        bool AnimSwitched = false;
        bool AnimNeedSwitch = false;

    private:
        static constexpr long long blackstoneProduceTime = 10800; //3 hours in seconds

        bool showing = false;

    public:
        virtual ~IntelligentCreatureDialog()
        {
            delete FullnessBG;
            delete FullnessFG;
            delete FullnessMin;
            delete FullnessNow;
            delete PearlImage;
            delete BlackStoneImageBG;
            delete BlackStoneImageFG;
            delete CreatureName;
            delete CreatureDeadline;
            delete CreaturePearls;
            delete CreatureInfo;
            delete CreatureInfo1;
            delete CreatureInfo2;
            delete CreatureMaintainFoodBuff;
            delete HoverLabel;
            delete CloseButton;
            delete HelpPetButton;
            delete CreatureRenameButton;
            delete SummonButton;
            delete DismissButton;
            delete ReleaseButton;
            delete AutomaticModeButton;
            delete SemiAutoModeButton;
            delete OptionsMenuButton;
            delete HoverLabelParent;
            delete CreatureImage;
        }

        IntelligentCreatureDialog();

        #pragma region EventHandlers
    private:
        void IntelligentCreatureDialog_BeforeDraw(std::any sender, EventArgs *e);
        void FullnessForeGround_AfterDraw(std::any sender, EventArgs *e);
        void BlackStoneImageFG_AfterDraw(std::any sender, EventArgs *e);

        void Control_MouseEnter(std::any sender, EventArgs *e);
        void Control_MouseLeave(std::any sender, EventArgs *e);

        void ButtonClick(std::any sender, EventArgs *e);

        #pragma endregion

        #pragma region Process
    public:
        void Update();
        void RefreshDialog();
    private:
        void RefreshInfo();
        void RefreshUI();
        void RefreshMode();

    public:
        int BeforeAfterDraw(); //No idea why.. but without this FullnessForeGround_AfterDraw wont work...

        #pragma region CreatureAnimation
    private:
        void DrawCreatureAnimation();

    public:
        int GetCreatureFromSlot(int slotidx);
        #pragma endregion

        #pragma endregion

        void SaveItemFilter(IntelligentCreatureItemFilter *filter);

        void OnMouseDown(MouseEventArgs *e) override;

        void Hide() override;
        void Show() override;
    };

    class CreatureButton final : public MirControl
    {
    public:
        MirImageControl *SelectionImage;
        MirLabel *NameLabel;
        MirButton *PetButton;
        IntelligentCreatureType PetType = IntelligentCreatureType::None;
        int idx = 0;
        bool Selected = false;

        int AnimDefaultIdx = 540;
        int AnimDefaultCount = 6;
        long long AnimDefaultDelay = 400;
        int AnimExIdx = 550;
        int AnimExCount = 5;
        long long AnimExDelay = 400;


        virtual ~CreatureButton()
        {
            delete SelectionImage;
            delete NameLabel;
            delete PetButton;
        }

        CreatureButton();

    private:
        void SetButtonInfo(ClientIntelligentCreature *pet);

    public:
        void Update(ClientIntelligentCreature *pet, bool setnew = false);

    private:
        void PetButtonClick(std::any sender, EventArgs *e);
        void PetButtonMouseEnter(std::any sender, EventArgs *e);
        void PetButtonMouseLeave(std::any sender, EventArgs *e);

    public:
        void SelectButton();
    private:
        void SelectButton(bool selection);

    public:
        void Clear();

    private:
        void SetCreatureFrames();
    };
    class IntelligentCreatureOptionsDialog final : public MirImageControl
    {
    public:
        const std::vector<std::string> OptionNames = {"All Items", "Gold", "Weapons", "Armours", "Helmets", "Boots", "Belts", "Jewelry", "Others"};
        IntelligentCreatureItemFilter *Filter;
        Point *locationOffset = new Point(450, 63);

        MirButton *OptionsSaveButton, *OptionsCancelButton;
        std::vector<MirCheckBox*> CreatureOptions;

        virtual ~IntelligentCreatureOptionsDialog()
        {
            delete Filter;
            delete locationOffset;
            delete OptionsSaveButton;
            delete OptionsCancelButton;
        }

        IntelligentCreatureOptionsDialog();

    private:
        void ButtonClick(std::any sender, EventArgs *e);
        void CheckBoxClick(std::any sender, EventArgs *e);

        void IntelligentCreatureOptionsDialog_BeforeDraw(std::any sender, EventArgs *e);

        void RefreshFilter();

    public:
        void Show(IntelligentCreatureItemFilter *filter);
    };
    class IntelligentCreatureOptionsGradeDialog final : public MirImageControl
    {
    private:
        std::vector<std::string> GradeStrings = {"All", "Common", "Rare", "Mythical", "Legendary"};

    public:
        MirButton *NextButton, *PrevButton;
        MirLabel *GradeLabel;
        int SelectedGrade = 0;
        ItemGrade GradeType = static_cast<ItemGrade>(0);

        Point *locationOffset = new Point(449, 39);

        virtual ~IntelligentCreatureOptionsGradeDialog()
        {
            delete NextButton;
            delete PrevButton;
            delete GradeLabel;
            delete locationOffset;
        }

        IntelligentCreatureOptionsGradeDialog();

    private:
        void Button_Click(std::any sender, EventArgs *e);

        Color *GradeNameColor(ItemGrade grade);

        // public override void OnMouseDown(MouseEventArgs e)
        //{
        //     GameScene.Scene.IntelligentCreatureOptionsDialog.BringToFront();
        //    base.OnMouseDown(e);
        // }

        void IntelligentCreatureOptionsGradeDialog_BeforeDraw(std::any sender, EventArgs *e);

    public:
        void RefreshGradeFilter();


        void Show(ItemGrade grade);
    };
}
